package com.allen.admin.service.server.manager.impl;

import com.allen.admin.service.common.domain.AdminPerm;
import com.xxx.dao.GenericDao;
import com.xxx.page.DataPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AdminPermManager {
	
	@Autowired
    private GenericDao dao;
	
	public AdminPerm insert(AdminPerm obj) {
		assert obj != null;
		
		obj.setCreatedTime(LocalDateTime.now());
		obj.setUpdatedTime(LocalDateTime.now());
		int affected = dao.insertAndReturnAffectedCount("AdminPermMapper.insert", obj);
		if (affected > 0) {
			return obj;
		} else {
			return null;
		}
	}

	public AdminPerm update(AdminPerm obj) {
		assert obj != null;
		
		obj.setUpdatedTime(LocalDateTime.now());
		int affected = dao.updateByObj("AdminPermMapper.update", obj);
		
		if (affected > 0) {
			return this.select(obj.getId());
		} else {
			return null;
		}
	}
	
	public AdminPerm select(Long id) {
		assert id != null;
		
		AdminPerm obj = dao.queryUnique("AdminPermMapper.selectById", id);
		return obj;
	}
	
	public List<AdminPerm> select(AdminPerm qObj) {
		assert qObj != null;
		
		Map<String, Object> map = new HashMap<>();
		if (qObj.getParent() != null) {
			map.put("parent", qObj.getParent());
		}
		if (qObj.getType() != null) {
			map.put("type", qObj.getType());
		}
		if (qObj.getStatus() != null) {
			System.out.print("what status is " + qObj.getStatus());
			map.put("status", qObj.getStatus());
		}
		
		
		return dao.queryList("AdminPermMapper.select", map);
	}
	
	public DataPage<AdminPerm> queryPage(DataPage<AdminPerm> dataPage, AdminPerm qObj) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("order", dataPage.getOrder());
		map.put("orderBy", dataPage.getOrderBy());
		
		if (qObj.getParent() != null) {
			map.put("parent", qObj.getParent());
		}
		if (qObj.getType() != null) {
			map.put("type", qObj.getType());
		}
		if (qObj.getStatus() != null) {
			map.put("status", qObj.getStatus());
		}
		
		dataPage = dao.queryPage(
				"AdminPermMapper.countPage", 
				"AdminPermMapper.queryPage", map, dataPage);
		
		return dataPage;
	}

}
